package com.ytjj.qmyx.admin.service.impl;

import com.ytjj.qmyx.admin.dao.ProductStarDao;
import com.ytjj.qmyx.admin.service.ProductStarService;
import com.ytjj.qmyx.mall.model.ProductStar;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 */
@Service
public class ProductStarServiceImpl implements ProductStarService {

	@Resource
	private ProductStarDao productStarDao;

	@Override
	public void syncInfo() {
		// 同步商品id -> 商品评星ProductStar
		List<Integer> productIdList = productStarDao.getProductIdList();
		for (Integer productId : productIdList) {
			// 随机生成价格、质量、实物相符星数 4~5之间的一位小数
			BigDecimal priceStar = new BigDecimal(Math.random() + 4).setScale(1, BigDecimal.ROUND_UP);
			BigDecimal qualityStar = new BigDecimal(Math.random() + 4).setScale(1, BigDecimal.ROUND_UP);
			BigDecimal matchStar = new BigDecimal(Math.random() + 4).setScale(1, BigDecimal.ROUND_UP);
			ProductStar productStar = new ProductStar();
			productStar.setProductId(productId);
			productStar.setPriceStar(priceStar);
			productStar.setQualityStar(qualityStar);
			productStar.setMatchStar(matchStar);
			productStarDao.addProductStar(productStar);
		}
	}

	@Override
	public ProductStar getStarInfo(Integer productId) {
		ProductStar productStar = productStarDao.getProductStar(productId);
		if(productStar == null) {
			ProductStar star = new ProductStar();
			star.setProductId(productId);
			// 随机生成价格、质量、实物相符星数 4~5之间的一位小数
			BigDecimal priceStar = new BigDecimal(Math.random() + 4).setScale(1, BigDecimal.ROUND_UP);
			star.setPriceStar(priceStar);
			BigDecimal qualityStar = new BigDecimal(Math.random() + 4).setScale(1, BigDecimal.ROUND_UP);
			star.setQualityStar(qualityStar);
			BigDecimal matchStar = new BigDecimal(Math.random() + 4).setScale(1, BigDecimal.ROUND_UP);
			star.setMatchStar(matchStar);
			productStarDao.addProductStar(star);
			return star;
		}
		return productStar;
	}

	@Override
	public void updateStarInfo(ProductStar productStar) {
		productStarDao.updateProductStar(productStar);
	}
}
